package pl.streamsoft.test.currency_view.storage;

import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;

import pl.streamsoft.test.currency_buisness.dto.CurrencyTypeDTO;

public class CurrencyTypeComboboxModel extends
		AbstractListModel<CurrencyTypeDTO> implements
		ComboBoxModel<CurrencyTypeDTO> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6796500800836413326L;
	private List<CurrencyTypeDTO> list;
	private CurrencyTypeDTO selectedItem;

	public CurrencyTypeComboboxModel() {
		list = new ArrayList<CurrencyTypeDTO>();
		clear();
	}

	public CurrencyTypeDTO getElementAt(int index) {

		return index <= list.size() ? list.get(index) : null;
	}

	public int getSize() {

		return list.size();
	}

	public Object getSelectedItem() {
		return contains(selectedItem) ? selectedItem : null;
	}

	public void setSelectedItem(Object obj) {
		selectedItem = (CurrencyTypeDTO) obj;
	}

	protected boolean contains(CurrencyTypeDTO obj) {
		return list.contains(obj);
	}

	public void clear() {
		list.clear();
		list.add(null);
		fireContentsChanged(this, 0, 1);
	}

	public void replaceElementsWith(List<CurrencyTypeDTO> currencyTypesList) {
		clear();
		if (currencyTypesList != null) {
			list.addAll(currencyTypesList);
		}
		fireContentsChanged(this, 0, getSize());
	}
}
